define(function(require,exports,module){

	require('jquery-validate');
    /**
     * 只支持字母格式
     * */
    jQuery.validator.addMethod("letter", function (value, element) {
        var reg = /^[a-zA-Z]+$/;
        return this.optional(element) || (reg.test(value));
    }, "只支持字母格式，如abc");
    /**
     * 只支持字母、数字格式
     * */
    jQuery.validator.addMethod("letterNum", function (value, element) {
        var reg = /^[a-zA-Z0-9]+$/;
        return this.optional(element) || (reg.test(value));
    }, "只支持字母、数字格式，如HT0120");
    /**
     * 只支持字母、数字格式、中、下横字符
     * */
    jQuery.validator.addMethod("letterNum_g", function (value, element) {
        var reg = /^[A-Za-z0-9_-]+$/;
        return this.optional(element) || (reg.test(value));
    }, "只支持字母、数字、下划线中划线格式，如H_T012-0");
    /**
     * 邮编格式
     * */
    jQuery.validator.addMethod("isZipCode", function (value, element) {
        var reg = /^[0-9]{6}$/;
        return this.optional(element) || (reg.test(value));
    }, "邮编格式不正确");

    /**
     * 电话号码验证（包括手机）
     * */
    $.validator.addMethod("isTel", function (value, element) {
        var tel = /^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9]+)?$/;
        var mobile = /^((1[3-8][0-9])+\d{8})$/;
        return this.optional(element) || (tel.test(value) || mobile.test(value));
    }, $.validator.format("请正确填写您的电话号码"));

    /**
     * 手机格式
     * */
    jQuery.validator.addMethod("isMobile", function (value, element) {
        var reg = /^((1[3-8][0-9])+\d{8})$/;
        return this.optional(element) || (reg.test(value));
    }, "请输入正确的手机号码");
    /**
     * 正整数
     * */
    jQuery.validator.addMethod("ispositivenum", function (value, element) {
        var reg = /^\d+$/;
        return this.optional(element) || (reg.test(value));
    }, "只能为正整数");
    /**
     * 日期格式
     * */
    jQuery.validator.addMethod("isdate", function (value, element) {
        var reg = /(((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9]))|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9]))|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9]))|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29))|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29))|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29))|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29))|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29))|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29))|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29))|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)))((\s+(0?[1-9]|1[012])(:[0-5]\d){0,2}(\s[AP]M))?$|(\s+([01]\d|2[0-3])(:[0-5]\d){0,2})?$))/;
        return this.optional(element) || (reg.test(value));
    }, "日期格式不正确");

    jQuery.validator.addMethod("noSpace", function (value, element) {
        var reg = /(^\s+)|(\s+$)/g;
        return this.optional(element) || (!reg.test(value));
    }, "不能含有空格");
    jQuery.validator.addMethod("zh_cn", function (value, element) {
        var reg = /^[\u4e00-\u9fa5]+$/i;
        return this.optional(element) || (reg.test(value));
    }, "只支持汉字");
    jQuery.validator.addMethod("usernameformat", function (value, element) {
        var reg = /^\w+$/;
        return this.optional(element) || (reg.test(value));
    }, "只能由数字,英文字母或者下划线组成");
    jQuery.validator.addMethod("isPassword", function (value, element) {
        var reg = /^([a-zA-Z]+\d+)|(\d+[a-zA-Z]+)|(\d+[~!@#$%_^&*.?]+)|([~!@#$%_^&*.?]+\d+)|([~!@#$%_^&*.?]+[a-zA-Z]+)|([a-zA-Z]+[~!@#$%_^&*.?]+)$/;
        var reg1 = /^([0-9a-zA-Z]|[~!@#$%_^&*.?])+$/;
        return this.optional(element) || (reg.test(value) && reg1.test(value));
    }, "英文字母、数字或符号，不能是纯数字，格式如a888888");

    /**
     * 6位数字的交易密码
     * */
    $.validator.addMethod("sixPwd", function (value, element) {
        var reg = /^\d{6,6}$/;
        return this.optional(element) || (reg.test(value));
    }, "请输入6位数字的交易密码");

    //身份证15位转18位中,计算校验位即最后一位
    function GetVerifyBit(id) {
        var result;
        var nNum = eval(id.charAt(0) * 7 + id.charAt(1) * 9 + id.charAt(2) * 10 + id.charAt(3) * 5 + id.charAt(4) * 8 + id.charAt(5) * 4 + id.charAt(6) * 2 + id.charAt(7) * 1 + id.charAt(8) * 6 + id.charAt(9) * 3 + id.charAt(10) * 7 + id.charAt(11) * 9 + id.charAt(12) * 10 + id.charAt(13) * 5 + id.charAt(14) * 8 + id.charAt(15) * 4 + id.charAt(16) * 2);
        nNum = nNum % 11;
        switch (nNum) {
            case 0 :
                result = "1";
                break;
            case 1 :
                result = "0";
                break;
            case 2 :
                result = "X";
                break;
            case 3 :
                result = "9";
                break;
            case 4 :
                result = "8";
                break;
            case 5 :
                result = "7";
                break;
            case 6 :
                result = "6";
                break;
            case 7 :
                result = "5";
                break;
            case 8 :
                result = "4";
                break;
            case 9 :
                result = "3";
                break;
            case 10 :
                result = "2";
                break;
        }
        //document.write(result);
        return result;
    }

    /*
     功能：验证身份证号码是否有效
     提 示信息：未输入或输入身份证号不正确！
     使用：validateIdCard(obj,birthday,s)//s：1为男，0为女
     返回：0,1,2,3,4,5
     */
    function validateIdCard(obj, birthday, s) {
        var aCity = {
            11: "北京",
            12: "天津",
            13: "河北",
            14: "山西",
            15: "内蒙古",
            21: "辽宁",
            22: "吉林",
            23: "黑龙 江",
            31: "上海",
            32: "江苏",
            33: "浙江",
            34: "安徽",
            35: "福建",
            36: "江西",
            37: "山东",
            41: "河南",
            42: "湖 北",
            43: "湖南",
            44: "广东",
            45: "广西",
            46: "海南",
            50: "重庆",
            51: "四川",
            52: "贵州",
            53: "云南",
            54: "西 藏",
            61: "陕西",
            62: "甘肃",
            63: "青海",
            64: "宁夏",
            65: "新疆",
            71: "台湾",
            81: "香港",
            82: "澳门",
            91: "国 外"
        };
        var iSum = 0;
        //var info = "";
        var strIDno = obj;
        if (birthday != null) {
            birthday = birthday.replace(/-/g, "/");
        }

        var idCardLength = strIDno.length;
        if (!/^\d{17}(\d|x)$/i.test(strIDno) && !/^\d{15}$/i.test(strIDno)) {
            return 1; //非法身份证号
        }
        if (aCity[parseInt(strIDno.substr(0, 2))] == null) {
            return 2;// 非法地区
        }

        // 15位身份证转换为18位
        if (idCardLength == 15) {
            sBirthday = "19" + strIDno.substr(6, 2) + "-" + Number(strIDno.substr(8, 2)) + "-" + Number(strIDno.substr(10, 2));
            var d = new Date(sBirthday.replace(/-/g, "/"));
            var dd = d.getFullYear().toString() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
            var genderNo = strIDno.substr(14, 1);
            if (sBirthday != dd) {
                return 3; //非法生日
            }
            if (birthday == "") {
                return 4;//您还没填写出生日期
            }

            if (birthday != null && d.getTime() != new Date(birthday).getTime()) {
                return 5; //与出生日期不符
            }
            if (s != null && s != 0 && s != 1) {
                return 6;//您还没填写性别
            }

            if (s != null && genderNo % 2 != s) {
                return 7;//与性别不符
            }

            strIDno = strIDno.substring(0, 6) + "19" + strIDno.substring(6, 15);
            strIDno = strIDno + GetVerifyBit(strIDno);
        }
        // 判断是否大于2078年，小于1900年
        var year = strIDno.substring(6, 10);
        if (year < 1900 || year > 2078) {
            return 3;//非法生日
        }
        /*if(){

         }*/
        //18位身份证处理
        //在后面的运算中x相当于数字10,所以转换成a
        strIDno = strIDno.replace(/x$/i, "a");

        sBirthday = strIDno.substr(6, 4) + "-" + Number(strIDno.substr(10, 2)) + "-" + Number(strIDno.substr(12, 2));
        var d = new Date(sBirthday.replace(/-/g, "/"));
        var genderNo = strIDno.substr(16, 1);
        if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) {
            return 3; //非法生日
        }
        if (birthday == "") {
            return 4;//您还没填写出生日期
        }
        if (birthday != null && d.getTime() != new Date(birthday).getTime()) {
            return 5; //与出生日期不符
        }

        if (s != null && s != 0 && s != 1) {
            return 6;//您还没填写性别
        }
        if (s != null && genderNo % 2 != s) {
            return 7;//与性别不符
        }
        // 身份证编码规范验证
        for (var i = 17; i >= 0; i--) {
            iSum += (Math.pow(2, i) % 11) * parseInt(strIDno.charAt(17 - i), 11);
        }
        if (iSum % 11 != 1) {
            return 1;// 非法身份证号
        }
        // 判断是否屏蔽身份证
        var words = new Array();
        words = new Array("11111119111111111", "12121219121212121");

        for (var k = 0; k < words.length; k++) {
            if (strIDno.indexOf(words[k]) != -1) {
                return 1;
            }
        }
        return 0;
    }

    //身份证（无关联验证）
    $.validator.addMethod("cretID", function (value, element, param) {

        var n = validateIdCard(value);
        var error = ["", "非法身份证号", "地区编号不合法", "出生日期不合法"];
        param[1] = error[n];

        return this.optional(element) || n == 0;

    }, $.validator.format("非法身份证号"));


    //银行卡号验证（简单）
    $.validator.addMethod("isBankNo", function (value, element) {
        var reg = /^[0-9]{16,19}$/;
        return this.optional(element) || (reg.test(value.replace(/[^\d]/g, '')));
    }, $.validator.format("银行卡号格式不正确"));

    //银行卡检验
    function luhmCheck(bankno) {
        if (bankno.length < 16 || bankno.length > 19) {
            //$("#banknoInfo").html("银行卡号长度必须在16到19之间");
            return 1;
            //return false;
        }
        var num = /^\d*$/;  //全数字
        if (!num.exec(bankno)) {
            //$("#banknoInfo").html("银行卡号必须全为数字");
            return 2;
            //return false;
        }
        //开头6位
        var strBin = "10,18,30,35,37,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,58,60,62,65,68,69,84,87,88,94,95,98,99";
        if (strBin.indexOf(bankno.substring(0, 2)) == -1) {
            //$("#banknoInfo").html("银行卡号开头6位不符合规范");
            return 3;
            //return false;
        }
        var lastNum = bankno.substr(bankno.length - 1, 1);//取出最后一位（与luhm进行比较）

        var first15Num = bankno.substr(0, bankno.length - 1);//前15或18位
        var newArr = new Array();
        for (var i = first15Num.length - 1; i > -1; i--) {    //前15或18位倒序存进数组
            newArr.push(first15Num.substr(i, 1));
        }
        var arrJiShu = new Array();  //奇数位*2的积 <9
        var arrJiShu2 = new Array(); //奇数位*2的积 >9

        var arrOuShu = new Array();  //偶数位数组
        for (var j = 0; j < newArr.length; j++) {
            if ((j + 1) % 2 == 1) {//奇数位
                if (parseInt(newArr[j]) * 2 < 9)
                    arrJiShu.push(parseInt(newArr[j]) * 2);
                else
                    arrJiShu2.push(parseInt(newArr[j]) * 2);
            }
            else //偶数位
                arrOuShu.push(newArr[j]);
        }

        var jishu_child1 = new Array();//奇数位*2 >9 的分割之后的数组个位数
        var jishu_child2 = new Array();//奇数位*2 >9 的分割之后的数组十位数
        for (var h = 0; h < arrJiShu2.length; h++) {
            jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
            jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
        }

        var sumJiShu = 0; //奇数位*2 < 9 的数组之和
        var sumOuShu = 0; //偶数位数组之和
        var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组个位数之和
        var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组十位数之和
        var sumTotal = 0;
        for (var m = 0; m < arrJiShu.length; m++) {
            sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
        }

        for (var n = 0; n < arrOuShu.length; n++) {
            sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
        }

        for (var p = 0; p < jishu_child1.length; p++) {
            sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
            sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
        }
        //计算总和
        sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);

        //计算Luhm值
        var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
        var luhm = 10 - k;

        if (lastNum == luhm) {
            //$("#banknoInfo").html("Luhm验证通过");
            return 0;
            //return true;
        }
        else {
            //$("#banknoInfo").html("银行卡号必须符合Luhm校验");
            return 4;
            //return false;
        }
        return 0;
    }


    //银行卡严格检验
    $.validator.addMethod("bankID", function (value, element, param) {
        var error = ["", "银行卡号长度必须在16到19之间", "银行卡号必须全为数字", "银行卡号开头6位不符合规范", "银行卡号必须符合Luhm校验"];
        var n = luhmCheck(value.replace(/[^\d]/g, ''));
        param[1] = error[n];
        return this.optional(element) || n == 0;
    }, "{1}");


    /**扩展类似format的方法实现定制信息**/
    /*$.validator.getName=function(){
     var arg=arguments;
     console.info(arguments);
     return "xiha";
     };*/

    /**
     * 表单验证规则扩展
     * @class addMethod
     *
     * 若要验证信息支持name属性，则要在表单元素加上data-name属性
     * 例如 <input type="text" name="area" data-name="仓库面积">
     * */
    var theregex = /\$?\{(\d+)\}/g;
    $.extend($.validator.messages, {
        required: function () {
            var name = arguments[1].getAttribute("data-name") == null ? "" : arguments[1].getAttribute("data-name");
            return "请填写" + name;
        },
        remote: function () {
            var name = arguments[1].getAttribute("data-name") == null ? "" : arguments[1].getAttribute("data-name");
            return "请填写" + name;
        },
        email: "请输入正确格式的电子邮件",
        url: "请输入合法的网址",
        isZipCode: "邮编格式不正确",

        date: "请输入合法的日期",
        dateISO: "请输入合法的日期",
        isdate: "日期格式不正确",

        number: "请输入合法的数字",
        digits: "请输入整数",
        ispositivenum: "请输入正整数",


        equalTo: function () {
            var name = arguments[1].getAttribute("data-name") == null ? "" : arguments[1].getAttribute("data-name");
            return "两次" + name + "输入不一致";
        },
        isPassword: "密码由6-16个字母、数字和符号组成，区分大小写，不能使用纯数字",
        noSpace: "不能含有空格",
        zh_cn: "请输入汉字",

        maxlength: $.validator.format("字符长度需小于{0}"),
        minlength: $.validator.format("字符长度需大于{0}"),
        rangelength: function () {
            var name = arguments[1].getAttribute("data-name") == null ? "" : arguments[1].getAttribute("data-name");
            var mes = $.validator.format(name + "需为{0}-{1}个字符", arguments[0]);
            return mes;
        },
        range: function () {
            var name = arguments[1].getAttribute("data-name") == null ? "" : arguments[1].getAttribute("data-name");
            var mes = $.validator.format(name + "需介于{0}-{1}", arguments[0]);
            return mes;
        },
        max: function () {
            var name = arguments[1].getAttribute("data-name") == null ? "" : arguments[1].getAttribute("data-name");
            var mes = $.validator.format(name + "需小于等于{0}", arguments[0]);
            return mes;
        },
        min: function () {
            var name = arguments[1].getAttribute("data-name") == null ? "" : arguments[1].getAttribute("data-name");
            var mes = $.validator.format(name + "需大于等于{0}", arguments[0]);
            return mes;
        },

        letter: "只支持字母格式，如abc",
        letterNum: "由字母、数字组成，如HT0120",
        usernameformat: "只能由数字,英文字母或者下划线组成",
        letterNum_g: "由字母、数字、下划线和中划线组成，如H_T012-0",

        isTel: "请输入正确的信用卡号",
        mobile: "请输入正确的手机号码",

        creditcard: "请输入合法的信用卡号",
        cretID: "请输入正确的身份证号码"
    });
});
